package com.ggb.grpc;

import com.ggb.proto.*;
import com.ggb.proto.StudentServiceGrpc;
import io.grpc.stub.StreamObserver;
import jdk.nashorn.internal.objects.annotations.Setter;

import java.util.UUID;

public class StudentServiceGrpcImpl extends StudentServiceGrpc.StudentServiceImplBase {
    @Override
    public void getRealNameByUserName(MyRequest request, StreamObserver<MyResponse> responseObserver) {
        System.out.println("client say: " + request.getUsername());
        responseObserver.onNext(MyResponse.newBuilder().setRealname("张三").build());
        responseObserver.onCompleted();
    }

    @Override
    public void getStudentByAge(StudentRequest request, StreamObserver<StudentResponse> responseObserver) {
        System.out.println("client age" + request.getAge());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(32).setCity("mianyang").setName("ggb").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(32).setCity("lisi").setName("ggb").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(32).setCity("wangwu").setName("ggb").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(32).setCity("zhaoliu").setName("ggb").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(32).setCity("laosi").setName("ggb").build());
        responseObserver.onNext(StudentResponse.newBuilder().setAge(32).setCity("laoe").setName("ggb").build());
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<StudentRequest> getStudentWrapperByAges(StreamObserver<StudentResponseList> responseObserver) {
        StudentResponseList build = StudentResponseList.newBuilder().build();
        return new StreamObserver<StudentRequest>() {
            @Override
            public void onNext(StudentRequest value) {
                System.out.println("onNext:" + value.getAge());
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());
            }

            @Override
            public void onCompleted() {
                StudentResponse studentResponse = StudentResponse.newBuilder().setName("zhangsan").setAge(23).setCity("mianyang").build();
                StudentResponse studentResponse2 = StudentResponse.newBuilder().setName("zhangsan").setAge(23).setCity("mianyang").build();
                StudentResponseList build1 = StudentResponseList.newBuilder().addStudentResponse(studentResponse).addStudentResponse(studentResponse2).build();
                responseObserver.onNext(build1);
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<StreamRequest> biTalk(StreamObserver<StreamResponse> responseObserver) {
        return new StreamObserver<StreamRequest>() {
            @Override
            public void onNext(StreamRequest value) {
                System.out.println(value.getRequestInfo());

                responseObserver.onNext(StreamResponse.newBuilder().setResponseInfo(UUID.randomUUID().toString()).build());

            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.getMessage());
            }

            @Override
            public void onCompleted() {
                responseObserver.onCompleted();
            }
        };
    }
}
